package com.hllxd.hism.star.basic.service.collect;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateSyncUtil {

    private static final Log log = LogFactory.getLog(DateSyncUtil.class);

    // 格式：yyyy-MM-dd HH:mm:ss
    static List<Map<String, String>> list = null;

    public static String dateFormated(Date date){
        //定义要输出日期字符串的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTimeSting  = sdf.format(date);
        return startTimeSting;
    }



    public static List convertList(ResultSet rs) throws SQLException, SQLException {
        List<Object> list = new ArrayList();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            Map rowData = new HashMap(10);
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }


    public ArrayList<String> pageSize(long totalRows,long pageSize){
        long num=totalRows%pageSize==0?totalRows/pageSize:totalRows/pageSize+1;
        ArrayList<String> numConbine = new ArrayList<String>();
        long start;
        long end;
        for (int i = 0; i <num; i++) {
            if (i==num-1){
                start=i*pageSize;
                end =totalRows;
                numConbine.add(start+","+end);
            }else {
                start=i*pageSize;
                long nums=(i+1)*pageSize-1;
                end=nums;
                numConbine.add(start+","+end);            }
        }
        return numConbine;
    }
    public String getLastHourStart(){
        //可以对每个时间域单独修改
        Calendar calen = Calendar.getInstance();
        calen.setTime(new Date());
        calen.set(Calendar.HOUR_OF_DAY, calen.get(Calendar.HOUR_OF_DAY) - 1);
        calen.set(Calendar.MINUTE, 0);
        calen.set(Calendar.SECOND, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time =format.format(calen.getTime());
        System.out.println(time);
        return time;
    }
    public String getLastHourEnd(){
        //可以对每个时间域单独修改
        Calendar calen = Calendar.getInstance();
        calen.setTime(new Date());
        calen.set(Calendar.HOUR_OF_DAY, calen.get(Calendar.HOUR_OF_DAY) - 1);
        calen.set(Calendar.MINUTE, 59);
        calen.set(Calendar.SECOND, 59);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time =format.format(calen.getTime());
        System.out.println(time);
        return time;
    }

    public static String getTodayZero(){
        //可以对每个时间域单独修改
        Calendar calen = Calendar.getInstance();
        calen.setTime(new Date());
        calen.set(Calendar.HOUR_OF_DAY, 0);
        calen.set(Calendar.MINUTE, 0);
        calen.set(Calendar.SECOND, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time =format.format(calen.getTime());
        System.out.println(time);
        return time;
    }

    public static String dateFormat(Date date){
        //定义要输出日期字符串的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTimeSting  = sdf.format(date);
        return startTimeSting;
    }

    public  String getLastday(){
        //可以对每个时间域单独修改
        Calendar calen = Calendar.getInstance();
        calen.setTime(new Date());
        calen.set(Calendar.DAY_OF_MONTH, calen.get(Calendar.DAY_OF_MONTH) - 1);
        calen.set(Calendar.MINUTE, 0);
        calen.set(Calendar.SECOND, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time =format.format(calen.getTime());
        return time;
    }
    public  String getToday(){
        //可以对每个时间域单独修改
        Calendar calen = Calendar.getInstance();
        calen.setTime(new Date());
        calen.set(Calendar.DAY_OF_MONTH, calen.get(Calendar.DAY_OF_MONTH));
        calen.set(Calendar.MINUTE, 0);
        calen.set(Calendar.SECOND, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time =format.format(calen.getTime());
        return time;
    }
    public static Map hbaseconvertList(ResultSet rs) throws SQLException{
        List<Object> list = new ArrayList();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        Map rowData = new HashMap(10);
        while (rs.next()) {
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
        }
        return rowData;
    }

    public static List<Map<String, String>> convertListMap(ResultSet rs,Integer num)  {
        list=new ArrayList();
        int columnCount = 1;
        ResultSetMetaData md = null;
        try {
            md = rs.getMetaData();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            columnCount= md.getColumnCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Map<String,String> rowData;
        try{
            log.info("开始导入数据到内存：======");
            while (rs.next()) {
                rowData= new HashMap<String,String>(10);
                for (int i = 1; i <=columnCount ; i++) {
                    rowData.put(md.getColumnName(i), rs.getString(i));
                }
                list.add(rowData);
            }
            log.info("开始导入数据到内存111：======");
        }catch (SQLException e) {
            log.error(e);
        }
        log.info("导入数据到内存完成：======");
        return list;
    }

    public String getDatabase(String url){
        int num=url.split("\\?")[0].split("/").length-1;
        String[] split = url.split("\\?")[0].split("/");
        System.out.println(split[num]);
        return split[num];
    }

    public String urlgetdatabase(String url){
        String s1 = url.split("\\?")[0].split("//")[1].split("/")[1];
        return s1;
    }

    public static String dateFormateds(Date date){
        //定义要输出日期字符串的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        String startTimeSting  = sdf.format(date);
        return startTimeSting;
    }


    public static Date utcToLoacl(final String str) throws ParseException {
        String tmp = str.replace("T"," ").replace("Z","");
        String pattern = "yyyy-MM-dd HH:mm:ss";
        if(tmp.contains(".")){
            String SSS = tmp.substring(str.indexOf(".") + 1);
            pattern += ".SSS";
            if(SSS.length() > 3){
                tmp = tmp.substring(0,str.indexOf(".")) + "." + SSS.substring(0,3);
            }
//			tmp = tmp.substring(0,str.indexOf("."));
        }
//		System.out.println(tmp + " --- " + pattern);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date utcDate = sdf.parse(tmp);
        sdf.setTimeZone(TimeZone.getDefault());
        String localTime = sdf.format(utcDate.getTime());
        return  sdf.parse(localTime);
    }

    /**时间戳秒级到纳秒级
     * @return
     */
    public Long getLongTime(){
        Date date = new Date();
        long time1 = date.getTime();
        return time1;

    }

    public void test(){
        Date date = new Date(System.currentTimeMillis());
        long time = date.getTime();
        System.out.println(time);
        Long longTime = getLongTime();
        System.out.println(longTime);
    }





}
